import { usePathname } from 'expo-router';
import { useAsyncEffect, useMemoizedFn, useSafeState } from 'ahooks';
import useUserModel from '@/models/user';

export const useAuth = () => {
  const pathname = usePathname();
  const logined = useUserModel(s => !!s.token);
  const checkToken = useUserModel(s => s.checkToken);
  const [loading, setLoading] = useSafeState(true);

  const onRouteChange = useMemoizedFn(async () => {
    await checkToken();
    setLoading(false);
  });

  useAsyncEffect(onRouteChange, [pathname]);
  return [logined, loading] as [boolean, boolean];
}